﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;


namespace pl.fromsoft.joomla.form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                textBox_selectedFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        /// <summary>
        /// Create XML button hit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            new pl.fromsoft.joomla.xmlifg.Program(getValues());
            toolStripStatusLabel1.Text = pl.fromsoft.joomla.xmlifg.Program.getMessage();

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StringBuilder menuSnippet = new StringBuilder();
            menuSnippet.Append("<menu>ComponentName</menu>" + System.Environment.NewLine);
            menuSnippet.Append("<submenu>" + System.Environment.NewLine);
            menuSnippet.Append(@"<menu img="" task="" controller="" view="" layout="" sub="">SubMenu1</menu>" + System.Environment.NewLine);
            menuSnippet.Append(@"<menu img="" task="" controller="" view="" layout="" sub="">SubMenu2</menu>" + System.Environment.NewLine);
            menuSnippet.Append(@"<menu img="" task="" controller="" view="" layout="" sub="">SubMenu3</menu>" + System.Environment.NewLine);
            menuSnippet.Append("</submenu>" + System.Environment.NewLine + System.Environment.NewLine);
            textBox_Xml.AppendText(menuSnippet.ToString());
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = string.Empty;
            saveFileDialog_xml.InitialDirectory = textBox_selectedFolder.Text;
            saveFileDialog_xml.FileName = "session.xml";

            DialogResult dr = saveFileDialog_xml.ShowDialog();
            if (dr == DialogResult.OK)
            {
                path = saveFileDialog_xml.FileName;

                pl.fromsoft.joomla.xmlifg.Program.SaveSession(getValues(), path);
            }
        }

        /// <summary>
        /// Return form field values.
        /// </summary>
        /// <returns>Form field values</returns>
        private SortedList getValues()
        {
            SortedList values = new SortedList();

            values.Add("folder", textBox_selectedFolder.Text);
            values.Add("name", textBox_name.Text);
            values.Add("version", textBox_version.Text);
            values.Add("creationdate", textBox_creationDate.Text);
            values.Add("author", textBox_author.Text);
            values.Add("authoremail", textBox_authorEmail.Text);
            values.Add("authorurl", textBox_authorUrl.Text);
            values.Add("copyright", textBox_copyright.Text);
            values.Add("license", textBox_license.Text);
            values.Add("description", textBox_description.Text);
            values.Add("sqlinstall", textBox_SqlInstall.Text);
            values.Add("sqluninstall", textBox_SqlUninstall.Text);
            values.Add("additionalxml", textBox_Xml.Text);

            return values;
        }

        /// <summary>
        /// Open session menu clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = string.Empty;
            openFileDialog_xml.InitialDirectory = textBox_selectedFolder.Text;

            DialogResult dr = openFileDialog_xml.ShowDialog();
            if (dr == DialogResult.OK)
            {
                path = openFileDialog_xml.FileName;

                SortedList values = pl.fromsoft.joomla.xmlifg.Program.OpenSession(path);
                if (values.Count > 0) recoverLastSession(values);
            }
        }

        private void recoverLastSession(SortedList values)
        {
            textBox_selectedFolder.Text = values["folder"].ToString();
            textBox_name.Text = values["name"].ToString();
            textBox_version.Text = values["version"].ToString();
            textBox_creationDate.Text = values["creationdate"].ToString();
            textBox_author.Text = values["author"].ToString();
            textBox_authorEmail.Text = values["authoremail"].ToString();
            textBox_authorUrl.Text = values["authorurl"].ToString();
            textBox_copyright.Text = values["copyright"].ToString();
            textBox_license.Text = values["license"].ToString();
            textBox_description.Text = values["description"].ToString();
            textBox_SqlInstall.Text = values["sqlinstall"].ToString();
            textBox_SqlUninstall.Text = values["sqluninstall"].ToString();
            textBox_Xml.Text = values["additionalxml"].ToString();
        }

        // Closing - save session
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"jxmlgen")))
                Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"jxmlgen"));

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"jxmlgen");
            pl.fromsoft.joomla.xmlifg.Program.SaveSession(getValues(), path+@"\session.xml");
        }

        // Form open - recover last session
        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "jxmlgen");
            SortedList values = pl.fromsoft.joomla.xmlifg.Program.OpenSession(path + @"\session.xml");
            if (values.Count > 0) recoverLastSession(values);
        }

    }
}
